package chapter4.eg6;

import java.time.Duration;
import java.util.Collections;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.junit.Test;

public class CustomDeserializerApp {
  @Test
  public void testDeserializer() throws InterruptedException {
    Properties properties = new Properties();
    properties.put("bootstrap.servers", "localhost:9092");
    properties.put("group.id", "CountryCounter1");
    properties.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
    properties.put("value.deserializer", "chapter4.eg6.CustomerDeserializer");
    KafkaConsumer<String, Customer> consumer = new KafkaConsumer<>(properties);
    consumer.subscribe(Collections.singletonList("customerCountries"));

    while (true) {
      ConsumerRecords<String, Customer> records = consumer.poll(Duration.ofMillis(100));
      for (ConsumerRecord<String, Customer> record : records) {
        Customer customer = record.value();
        System.out.println("current customer id: " + customer.getId() + " and name: " + customer.getName());
      }

      TimeUnit.SECONDS.sleep(1);
    }
  }

}
